package com.antdesignbackend.backend.edition.controller;

import java.io.Serializable;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.edition.entity.Edition;
import com.antdesignbackend.backend.edition.entity.entityVo.EditionVo;
import com.antdesignbackend.backend.edition.service.EditionService;

/**
 * 版本升级
 *
 * @author 林宇杰
 * @email edition
 * @date 2020-10-12 14:17:52
 */

@Controller
@RequestMapping("/edition")
@Slf4j
public class EditionController extends BaseController{

	private static final String FUN_NAME = "版本升级";//功能名称

	@Autowired
	private EditionService editionService;

//	@GetMapping("/index")
//	@AuthToken
//	public String index() {
//		return "edition/edition/index";
//	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody EditionVo editionVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<Edition> wrapper = new QueryWrapper<>();
		Page<Edition> page = new Page<>(editionVo.getPageIndex(), editionVo.getPageSize());
		IPage<Edition> pageResult = editionService.page(page, wrapper);
		result.setTotal((int)pageResult.getTotal());
		result.setData(pageResult.getRecords());
		return result;

	}

	@PostMapping("/tableSearch2")
	@ResponseBody
	ReturnVO tableSearch2(@RequestBody EditionVo editionVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<Edition> wrapper = new QueryWrapper<>();
		wrapper.orderByDesc("upgradeTime");
		Page<Edition> page = new Page<>(editionVo.getPageIndex(), editionVo.getPageSize());
		IPage<Edition> pageResult = editionService.page(page, wrapper);
		result.setData(pageResult.getRecords().get(0));
		return result;
	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public	Edition getById(@PathVariable(name = "id") String id) {
		Edition obj = editionService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody Edition edition){
		ReturnVO result = new ReturnVO();
		if(editionService.save(edition)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody Edition edition){
		ReturnVO result = new ReturnVO();
		if(editionService.updateById(edition)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (editionService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

}
